// pages/answer/answer.js
Page({
  data: {
    contentInput:"",
    id:0,
    title:'',
    content:''
  },
  reset:function() {
    this.setData({
      contentInput:"",
    })
  },
  browse: function () {
    var that=this;
    wx.chooseImage({
      count: 1,
      success:function(res){
        var tempFilePaths=res.tempFilePaths
        that.setData({
          picUrl:tempFilePaths[0],
          haveapicture:false
        })
        //console.log(res.tempFilePaths)
         //  跳转页面编辑图片
         wx.navigateTo({
          url: '/pages/piccut/piccut?type='+2+'&src='+tempFilePaths[0],
        })
      }
    })
  },
  write:function(e) {
    this.setData({
      contentInput:e.detail.value
    })
  },
  formSubmit: function () {
    var that=this;
    var content=this.data.contentInput;
    let right=this.data.content
    var Title=that.data.title;
    var id=that.data.id;
    console.log(content);

    let rightSet=right.split(' ')
    let rightNum=0
    let contentList=new Set(content.split(' '))
    let compare=[]
    for(let i=0;i<rightSet.length;i++){
      if(contentList.has(rightSet[i])){
        rightNum++
        compare.push({'word':rightSet[i],'colorType':1})
      }else{
        compare.push({'word':rightSet[i],'colorType':0})
      }
    }
    let score=Math.floor(rightNum/rightSet.length*100)
    console.log(score,compare)

    wx.request({
      url: 'http://106.15.67.178:8080/createAnswer',
      data: {
        'workId':that.data.id,
        'correctAnswer':right,
        'score':score,
        'commitAnswer':content,
        'wordCount':rightNum,
        'scoreStatus':JSON.stringify(compare)
      },
      method: 'POST', 
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res.data);
        // if(res.data.success==true){
        wx.showToast({
          title: '提交成功',
          icon:'success',
          duration:2000,
          success: function(res) {
           wx.reLaunch({
             url: '/pages/workRaport/workRaport?title='+Title+'&id='+id+'&content='+content,
           })
          }
        })
      // }
      },
      fail: function (res) {
        console.log('cuowu' + ':' + res)
      }
    })
  },
  
/**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id,
      title:options.title,
      content:options.content
    })
    // console.log(options.id);
    console.log(this.data.content)
  }

})